/**
 * 头像URL处理工具函数
 */

/**
 * 清理头像URL，确保URL格式正确
 * @param {string} avatarUrl - 原始头像URL
 * @returns {string} - 清理后的头像URL
 */
function cleanAvatarUrl(avatarUrl) {
  if (!avatarUrl) {
    return "";
  }

  // 清理重复的域名和@符号
  let cleanedUrl = avatarUrl
    .replace(/^@/, "") // 移除开头的@符号
    .replace(
      /https:\/\/jianianhua\.asiahttps:\/\/jianianhua\.asia/g,
      "https://jianianhua.asia"
    ) // 移除重复的域名
    .replace(
      /https:\/\/jianianhua\.asiahttps:\/\/jianianhua\.asia/g,
      "https://jianianhua.asia"
    ); // 再次清理，防止多次重复

  // 如果URL以/开头，添加域名
  if (cleanedUrl.startsWith("/")) {
    cleanedUrl = "https://jianianhua.asia" + cleanedUrl;
  }

  return cleanedUrl;
}

/**
 * 检查头像URL是否有效
 * @param {string} avatarUrl - 头像URL
 * @returns {boolean} - 是否有效
 */
function isValidAvatarUrl(avatarUrl) {
  if (!avatarUrl) {
    return false;
  }

  // 检查是否是有效的URL格式
  try {
    new URL(avatarUrl);
    return true;
  } catch {
    return false;
  }
}

module.exports = {
  cleanAvatarUrl,
  isValidAvatarUrl,
};
